Announcement

Collapse
No announcement yet.
X
  • Filter
  • Time
  • Show
Clear All
new posts

  • Excluding*data entries

    Hi,

    I have something that is confusing me. I have a dataset and want to merge it with another, but before I can do that I have to drop all entries which are the same personID, same varA, but different varB.

    I want to table 1 to become table 2.

    Is there a way to do this?

    I have been trying to use this explanation, https://www.stata.com/support/faqs/d...-observations/ but have not been able to get it to work yet.

    Thanks.

    Table 1
    PersonID varA varB
    1 a ab
    1 a ab
    2 c cd
    3 a ab
    3 a ac
    4 z zy
    4 z zy
    5 z zy
    5 z zx
    Table 2
    PersonID varA varB
    1 a ab
    1 a ab
    2 c cd
    4 z zy
    4 z zy

  • #2
    Code:
    * Example generated by -dataex-. To install: ssc install dataex
    clear
    input byte personid str1 vara str2 varb
    1 "a" "ab"
    1 "a" "ab"
    2 "c" "cd"
    3 "a" "ab"
    3 "a" "ac"
    4 "z" "zy"
    4 "z" "zy"
    5 "z" "zy"
    5 "z" "zx"
    end
     
    egen group= group(vara)
    bys group (personid): gen fob=_n==1
    bys personid: egen tokeep = max(fob)
    keep if tokeep
    Result:

    Code:
    . l
    
         +-----------------------------------------------+
         | personid   vara   varb   group   fob   tokeep |
         |-----------------------------------------------|
      1. |        1      a     ab       1     1        1 |
      2. |        1      a     ab       1     0        1 |
      3. |        2      c     cd       2     1        1 |
      4. |        4      z     zy       3     0        1 |
      5. |        4      z     zy       3     1        1 |
         +-----------------------------------------------+

    Comment

    Working...
    X